Page({
  onLoad: function (options) {
    console.log(options)
    this.getData(options.params)
  },
  getData: function (payParams) {
    var that = this;
    var params = JSON.parse(unescape(payParams));
    that.pay(params)

  },
  pay: function (payParams) {
    console.log(payParams)
    var that = this
    wx.requestPayment({
      timeStamp: payParams.timeStamp.toString(),
      nonceStr: payParams.nonceStr,
      package: payParams.package,
      signType: 'MD5',
      paySign: payParams.paySign,
      success: function (res) {
        // 支付成功
        // that.navback()
        wx.navigateTo({
          url: '../showResult/pay?ispaysuccess=' + payParams.show_type + '&id=' + payParams.id + '&timestap=' + payParams.timeStamp, 
        })
      },
      fail: function (res) {
        var type = 4;
        if (payParams.show_type == 3) {
          type = 5;
        }
        if (res.errMsg === "requestPayment:fail cancel") {
          // 用户取消支付
          // that.navback()
          wx.navigateTo({
            url: '../showResult/pay?ispaysuccess=' + type + '&timestap=' + payParams.timeStamp, 
          })
          return
        }
        if (res.errMsg === "requestPayment:fail") {
          console.log(res.err_desc) // 错误信息
          // that.navback()
          wx.navigateTo({
            url: '../showResult/pay?ispaysuccess=' + type + '&timestap=' + payParams.timeStamp, 
          })
          return
        }
      }
    })
  },
  navback: () => {
    setTimeout(function () {
      wx.navigateBack({ delta: 1 })
    }, 1000)
  }
})
